package com.whfc.quality.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.DateUtil;
import com.whfc.quality.dao.QualityAcceptanceImgMapper;
import com.whfc.quality.dao.QualityAcceptanceMapper;
import com.whfc.quality.dto.QualityAcceptanceDTO;
import com.whfc.quality.entity.QualityAcceptance;
import com.whfc.quality.param.QualityAcceptanceAddParam;
import com.whfc.quality.param.QualityAcceptanceEditParam;
import com.whfc.quality.service.QualityAcceptanceService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author: hw
 * @date: 2021-10-29 18:07
 * @description: 验收情况
 */
@DubboService(interfaceClass = QualityAcceptanceService.class, version = "1.0.0")
public class QualityAcceptanceServiceImpl implements QualityAcceptanceService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private QualityAcceptanceMapper qualityAcceptanceMapper;

    @Autowired
    private QualityAcceptanceImgMapper qualityAcceptanceImgMapper;

    @Override
    public void add(QualityAcceptanceAddParam param) throws BizException {
        logger.info("新增验收情况，param：{}", param);
        QualityAcceptance record = new QualityAcceptance();
        BeanUtils.copyProperties(param, record);
        record.setCreateTime(param.getTime());
        qualityAcceptanceMapper.insertSelective(record);
        if (param.getImgUrls() != null && !param.getImgUrls().isEmpty()) {
            qualityAcceptanceImgMapper.insertAll(record.getId(), record.getDeptId(), param.getImgUrls());
        }

    }

    @Override
    public void edit(QualityAcceptanceEditParam param) throws BizException {
        logger.info("修改验收情况，param：{}", param);
        QualityAcceptance record = qualityAcceptanceMapper.selectByPrimaryKey(param.getAcceptanceId());
        if (record == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "需要修改的数据不存在");
        }
        BeanUtils.copyProperties( record,param);
        qualityAcceptanceMapper.updateByPrimaryKeySelective(record);
        qualityAcceptanceImgMapper.deleteByAcceptanceId(param.getAcceptanceId());
        if (param.getImgUrls() != null && !param.getImgUrls().isEmpty()) {
            qualityAcceptanceImgMapper.insertAll(record.getId(), record.getDeptId(), param.getImgUrls());
        }
    }

    @Override
    public QualityAcceptanceDTO details(Integer deptId, Date time) throws BizException {
        logger.info("查询验收情况，deptId：{}，time：{}", deptId, time);
        Date startTime = DateUtil.getDateBegin(time);
        Date endTime = DateUtil.getDateEnd(time);
        QualityAcceptanceDTO data = qualityAcceptanceMapper.selectByDeptId(deptId, startTime, endTime);
        if (data != null) {
            List<String> imgUrls = qualityAcceptanceImgMapper.selectByAcceptanceId(data.getAcceptanceId());
            data.setImgUrls(imgUrls);
        }
        return data;
    }
}
